using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using SharpCore.Data;
using SharpCore.Utilities;

namespace Dating.DA
{
	public class MemberPhotoDA
	{
		#region Fields

		protected string connectionStringName;

		#endregion

		#region Constructors
		public MemberPhotoDA()
		{
		}

		public MemberPhotoDA(string connectionStringName)
		{
			ValidationUtility.ValidateArgument("connectionStringName", connectionStringName);

			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the MemberPhoto table.
		/// </summary>
		public  int Insert(MemberPhoto memberPhoto)
		{
			ValidationUtility.ValidateArgument("memberPhoto", memberPhoto);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", memberPhoto.TenantID),
				new SqlParameter("@ProfileID", memberPhoto.ProfileID),
				new SqlParameter("@Title", memberPhoto.Title),
				new SqlParameter("@IsPrimary", memberPhoto.IsPrimary),
				new SqlParameter("@PhotoURL", memberPhoto.PhotoURL),
				new SqlParameter("@IsApproved", memberPhoto.IsApproved),
				new SqlParameter("@AddedOn", memberPhoto.AddedOn)
			};

			memberPhoto.PhotoID = Convert.ToInt32(SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "MemberPhotoInsert", parameters));
			return memberPhoto.PhotoID;
		}

		/// <summary>
		/// Updates a record in the MemberPhoto table.
		/// </summary>
		public  int Update(MemberPhoto memberPhoto)
		{
			ValidationUtility.ValidateArgument("memberPhoto", memberPhoto);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@PhotoID", memberPhoto.PhotoID),
				new SqlParameter("@TenantID", memberPhoto.TenantID),
				new SqlParameter("@ProfileID", memberPhoto.ProfileID),
				new SqlParameter("@Title", memberPhoto.Title),
				new SqlParameter("@IsPrimary", memberPhoto.IsPrimary),
				new SqlParameter("@PhotoURL", memberPhoto.PhotoURL),
				new SqlParameter("@IsApproved", memberPhoto.IsApproved),
				new SqlParameter("@AddedOn", memberPhoto.AddedOn)
			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "MemberPhotoUpdate", parameters));
		}

		/// <summary>
		/// Deletes a record from the MemberPhoto table by its primary key.
		/// </summary>
		public  int Delete(int photoID, int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@PhotoID", photoID)
,				new SqlParameter("@TenantID", tenantID)
			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "MemberPhotoDelete", parameters));
		}

		/// <summary>
		/// Deletes a record from the MemberPhoto table by a foreign key.
		/// </summary>
		public  int DeleteAllByPhotoID(int photoID, int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@PhotoID", photoID)
,				new SqlParameter("@TenantID", tenantID)
			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "MemberPhotoDeleteAllByPhotoID", parameters));
		}

		/// <summary>
		/// Deletes a record from the MemberPhoto table by a foreign key.
		/// </summary>
		public  int DeleteAllByTenantID(int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", tenantID)

			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "MemberPhotoDeleteAllByTenantID", parameters));
		}

		/// <summary>
		/// Selects a single record from the MemberPhoto table.
		/// </summary>
		public  MemberPhoto Select(int photoID, int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@PhotoID", photoID)
,				new SqlParameter("@TenantID", tenantID)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "MemberPhotoSelect", parameters))
			{
				if (dataReader.Read())
				{
					return MakeMemberPhoto(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects all records from the MemberPhoto table.
		/// </summary>
		public  List<MemberPhoto> SelectAll(int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", tenantID)
			};
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "MemberPhotoSelectAll", parameters))
			{
				List<MemberPhoto> memberPhotoList = new List<MemberPhoto>();
				while (dataReader.Read())
				{
					MemberPhoto memberPhoto = MakeMemberPhoto(dataReader);
					memberPhotoList.Add(memberPhoto);
				}

				return memberPhotoList;
			}
		}

		/// <summary>
		/// Selects all records from the MemberPhoto table by a foreign key.
		/// </summary>
		public  List<MemberPhoto> SelectAllByPhotoID(int photoID, int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@PhotoID", photoID)
,				new SqlParameter("@TenantID", tenantID)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "MemberPhotoSelectAllByPhotoID", parameters))
			{
				List<MemberPhoto> memberPhotoList = new List<MemberPhoto>();
				while (dataReader.Read())
				{
					MemberPhoto memberPhoto = MakeMemberPhoto(dataReader);
					memberPhotoList.Add(memberPhoto);
				}

				return memberPhotoList;
			}
		}

		/// <summary>
		/// Selects all records from the MemberPhoto table by a foreign key.
		/// </summary>
		public  List<MemberPhoto> SelectAllByTenantID(int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", tenantID)

			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "MemberPhotoSelectAllByTenantID", parameters))
			{
				List<MemberPhoto> memberPhotoList = new List<MemberPhoto>();
				while (dataReader.Read())
				{
					MemberPhoto memberPhoto = MakeMemberPhoto(dataReader);
					memberPhotoList.Add(memberPhoto);
				}

				return memberPhotoList;
			}
		}

		/// <summary>
		/// Creates a new instance of the MemberPhoto class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected  MemberPhoto MakeMemberPhoto(SqlDataReader dataReader)
		{
			MemberPhoto memberPhoto = new MemberPhoto();
			memberPhoto.PhotoID = SqlClientUtility.GetInt32(dataReader, "PhotoID", 0);
			memberPhoto.TenantID = SqlClientUtility.GetInt32(dataReader, "TenantID", 0);
			memberPhoto.ProfileID = SqlClientUtility.GetInt32(dataReader, "ProfileID", 0);
			memberPhoto.Title = SqlClientUtility.GetString(dataReader, "Title", String.Empty);
			memberPhoto.IsPrimary = SqlClientUtility.GetString(dataReader, "IsPrimary", String.Empty);
			memberPhoto.PhotoURL = SqlClientUtility.GetString(dataReader, "PhotoURL", String.Empty);
			memberPhoto.IsApproved = SqlClientUtility.GetString(dataReader, "IsApproved", String.Empty);
			memberPhoto.AddedOn = SqlClientUtility.GetDateTime(dataReader, "AddedOn", DateTime.Now);

			return memberPhoto;
		}

		#endregion
	}
}
